#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int total = 10;
bool **  Tabla;

/**
Crea una matriz de bool
*/

void Create ()
{
   Tabla = new bool * [total];
   for (int i = 0; i < total ; i++)
   {
       Tabla[i] = new bool [total];
   }
}


/**
Carga de un archivo la Tabla y lo convierte a una tabla de bool
*/
void Load ()
{
    char buffer[100];
    ifstream ar ("tabla2");
    vector <string> vec;
    while(!ar.eof())
    {
       ar >> buffer;
       vec.push_back(buffer);
    }
    vec.pop_back();

    for (int i = 0; i < vec.size(); i++)
    {
       string cadena = vec[i];
       for (int j = 0; j < cadena.length(); j++)
       {
          if (cadena[j] == '*') Tabla[i][j] = 1;
          else Tabla[i][j] = 0;
       }

    }
}


/**
Inicializa las celdas
*/
void initialize ()
{
   Create();
   Load();
}

/**
Imprime la tabla en funcion a 0s o 1s
*/
void print_Ad ()
{
    for (int i = 0; i < total ; i++)
    {
        for (int j = 0; j < total; j++)
        {
            cout << Tabla[i][j] ;
        }
        cout << endl;
    }
}

/**
Imprime el tablero
*/
void print()
{
   for (int i = 0; i < total ; i++)
   {
       for (int j = 0; j < total ; j++)
       {
          if(Tabla[i][j] == 1)
          {
              cout << " * " ;
          }
          else
          {
              cout << " . ";
          }
       }
       cout << endl;
   }
   cout << endl ;
}



/**
Cuenta los vecinos de una celda dada
*/
int count_neighbors (int i , int j)
{
  int vecinos;
   if (i == 0 && j == 0)
   {
     vecinos = Tabla[i][j+1] + Tabla[i+1][j] + Tabla[i+1][j+1];
     return vecinos;
   }

   if (i == 0 && j == total-1)
   {
     vecinos = Tabla[i][j-1] + Tabla[i+1][j-1] + Tabla[i+1][j];
     return vecinos;
   }

   if(i== total-1 && j == 0)
   {
     vecinos = Tabla[i-1][j] + Tabla[i-1][j+1] + Tabla[i][j+1];
     return vecinos;
   }

   if(i == total-1 && j == total-1)
   {
     vecinos = Tabla[i-1][j-1] + Tabla[i-1][j] + Tabla[i][j-1];
     return vecinos;
   }

   if(j == 0)
   {
     vecinos = Tabla[i-1][j] + Tabla[i-1][j+1] + Tabla[i][j+1] + Tabla[i+1][j+1] + Tabla[i+1][j];
     return vecinos;
   }

   if(j == total-1)
   {
     vecinos = Tabla[i-1][j] + Tabla[i-1][j-1] + Tabla[i][j-1] + Tabla[i+1][j-1] + Tabla[i+1][j];
     return vecinos;
   }

   if (i == 0)
   {
     vecinos = Tabla[i][j-1] + Tabla[i+1][j-1] + Tabla[i+1][j] + Tabla[i+1][j+1] + Tabla[i][j+1];
     return vecinos;
   }

   if (i==total-1)
   {
     vecinos = Tabla[i][j-1] + Tabla[i-1][j-1] + Tabla[i-1][j] + Tabla[i-1][j+1] + Tabla[i][j+1];
     return vecinos;
   }

   else
   {
     vecinos = Tabla[i-1][j-1] + Tabla[i-1][j] + Tabla[i-1][j+1] + Tabla[i][j+1] + Tabla[i+1][j+1] + Tabla[i+1][j] + Tabla[i+1][j-1] + Tabla[i][j-1] ;
     return vecinos;
   }
}

/**
Simula el siguiente estado
*/
void iterate()
{

   for (int i = 0; i < total ; i++)
   {
       for (int j = 0; j < total ; j++)
       {
           if ((count_neighbors (i , j) < 2) && (count_neighbors (i , j) >= 0) && Tabla[i][j] == 1)
           {
              Tabla[i][j] = 0;
           }

           if ((count_neighbors (i , j) > 3) && Tabla[i][j] == 1)
           {
              Tabla[i][j] = 0;
           }

           if ((count_neighbors (i , j) == 3) && Tabla[i][j] == 0)
           {
               Tabla[i][j] = 1;
           }
       }
   }
}


/**
*/
void simulate_life (int count)
{
    for (int i = 0; i < count ; i++)
    {
       print();
       iterate();
    }
}

int main()
{
    initialize();
    simulate_life (20);
    cout << endl << endl;


    return 0;
}
